<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <crudOperation :permission="permission">
        <el-button
          slot="left"
          class="filter-item"
          size="mini"
          type="primary"
          @click="crud.toDetail('feePayDetail')"
          v-permission="permission.add"
        >
          新增
        </el-button>
        <el-button
          slot="left"
          class="filter-item"
          size="mini"
          type="danger"
          :loading="crud.rejectLoading"
          icon="el-icon-remove-outline"
          @click="audit('PASS','审核')"
          :disabled="crud.selections.length < 1"
          v-permission="permission.pass"
        >
          审核
        </el-button>
        <el-button
          slot="left"
          class="filter-item"
          size="mini"
          :loading="crud.rejectLoading"
          type="success"
          icon="el-icon-circle-check"
          :disabled="crud.selections.length < 1"
          @click="audit('REJECT','反审')"
          v-permission="permission.reject"
        >
          反审
        </el-button>
        <el-button
          slot="left"
          class="filter-item"
          size="mini"
          :loading="crud.rejectLoading"
          type="success"
          icon="el-icon-circle-check"
          :disabled="crud.selections.length < 1"
          @click="exportData()"
          v-permission="permission.reject"
        >
          导出照片
        </el-button>
      </crudOperation>
      <div v-if="!crud.props.searchToggle">
        <el-form size="small" label-width="70px" v-formkey="{query:toQuery}">
          <el-row :gutter="10">
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="业务日期">
                <div class="flex flex-row">
                  <el-date-picker
                    v-model="query.startTime"
                    align="right"
                    unlink-panels
                    class="fl1"
                    format="yyyy - MM - dd "
                    value-format="yyyy-MM-dd"
                    default-value
                    range-separator=""
                    placeholder="开始日期"
                  />
                  <el-date-picker
                    v-model="query.endTime"
                    align="right"
                    unlink-panels
                    class="fl1"
                    format="yyyy - MM - dd "
                    value-format="yyyy-MM-dd"
                    default-value
                    range-separator=""
                    placeholder="结束日期"
                  />
                </div>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="单据编号">
                <el-input v-model="query.code" placeholder="请输入单据编号"></el-input>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="往来单位">
                <zSelect :value.sync="query.customerId" :label="query.customerName"
                         url="api/dropDown/basCustomerList" style="width: 100%;"/>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="部门">
                <zSelect :value.sync="query.deptId" :label="query.deptName"
                         url="api/dropDown/childSysDeptList"
                         @change="changeDept"
                         style="width:  100%"/>
              </el-form-item>
            </el-col>
            </el-row>
            <el-row :gutter="10" style="margin-bottom: 5px">
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="业务员">
                <zSelect :value.sync="query.userId" :label="query.userName" url="api/dropDown/sysUserList"
                         :params="userParams"
                         style="width:  100%;"/>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="支付类型">
                <el-select v-model="query.type" class="filter-item" clearable placeholder="状态" size="small"
                           style="width: 100%;">
                  <el-option
                    v-for="item in dict.t_fee_pay_type"
                    :key="item.id"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="备注">
                <el-input v-model="query.remark" placeholder="请输入备注"></el-input>
              </el-form-item>
            </el-col>
              <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
                <el-form-item label="往来单位">
                  <el-input v-model="query.customerKeyword" placeholder="请输入往来单位名称编码/助记符"></el-input>
                </el-form-item>
              </el-col>
            <el-col :span="6" align="center">
              <rrOperation :crud="crud"/>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <!--表格渲染-->
      <el-table ref="table"
                @row-click="crud.selectRow"
                :row-style="crud.rowClass"
                :row-class-name="crud.tableRowClassName"
                v-if="isShowTable"
                @header-dragend="changeColWidth"
                @contextmenu.native="showMenu"
                @sort-change="sortTab"
                border v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;"
                @row-dblclick="crud.toDetailPlus('feePayDetail',$event.id)"
                @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55"/>
        <el-table-column type="index" width="60" label="序号"/>
        <el-table-column
          v-if="item.visible"
          v-for="(item, index) in crud.header"
          :key="`col_${index}`"
          :prop="item.prop"
          :label="columnLabel(item)"
          :width="item.width + 'px'"
          :data-porp='item.prop'
          @contextmenu.native="showMenu"
          @header-dragend="changeColWidth"
          :sortable="item.sortable?'custom':false"
          :sort-orders="['descending', 'ascending', null]"
          sort-change="sort"
        >
          <!-- 自定义渲染模版 -->
          <template slot-scope="scope">
            <!-- 当列key为 sort 时，渲染一个手柄icon -->
            <template v-if="item.prop =='sort'">
              <div class="handle_move">
                <i :class="scope.row['sort']" />
              </div>
            </template>
            <template v-else>
              <span v-if="item.type == 1" style="margin-left: 10px">{{scope.row[`${item.prop}`]}}</span>
              <span v-if="item.type == 2" style="margin-left: 10px">{{ parseTime(scope.row[`${item.prop}`])}}</span>
              <span v-if="item.type == 3" style="margin-left: 10px">{{ getDict(`this.dict.label.${item.dictionary}["${scope.row[item.prop]}"]`) }}</span>
            </template>
          </template>
        </el-table-column>
<!--        <el-table-column width="120" v-if="columns.visible('busDate')" prop="busDate" label="业务日期"/>-->
<!--        <el-table-column width="160" v-if="columns.visible('code')" prop="code" label="单据编号"/>-->
<!--        <el-table-column width="160" v-if="columns.visible('customerName')" prop="customerName" label="往来单位"/>-->
<!--        <el-table-column width="120" v-if="columns.visible('deptName')" prop="deptName" label="部门"/>-->
<!--        <el-table-column width="120" v-if="columns.visible('userName')" prop="userName" label="业务员"/>-->
<!--        <el-table-column width="100" v-if="columns.visible('type')" prop="type" label="支付类型">-->
<!--          <template slot-scope="scope">-->
<!--            <span v-for="item in dict.t_fee_pay_type" v-if="item.value === scope.row.type">{{-->
<!--                item.label-->
<!--              }}</span>-->
<!--          </template>-->
<!--        </el-table-column>-->
<!--        <el-table-column width="160" v-if="columns.visible('payAmount')" prop="payAmount" label="支付金额"/>-->
<!--        <el-table-column width="100" v-if="columns.visible('type')" prop="type" label="来源类型">-->
<!--          <template slot-scope="scope">-->
<!--            <span v-for="item in dict.t_fee_pay_source_type" v-if="item.value == scope.row.sourceType">{{-->
<!--                item.label-->
<!--              }}</span>-->
<!--          </template>-->
<!--        </el-table-column>-->
<!--        <el-table-column width="160" v-if="columns.visible('sourceCode')" prop="sourceCode" label="来源单号"/>-->
<!--        <el-table-column width="80" v-if="columns.visible('orderStatus')" prop="orderStatus" label="状态">-->
<!--          <template slot-scope="scope">-->
<!--            <span v-for="item in dict.common_order_status" v-if="item.value == scope.row.orderStatus">{{-->
<!--                item.label-->
<!--              }}</span>-->
<!--          </template>-->
<!--        </el-table-column>-->
<!--        <el-table-column width="160" v-if="columns.visible('remark')" prop="remark" label="备注"/>-->
<!--        <el-table-column fixed="right" v-permission="['admin','feePay:edit','feePay:del']" label="操作" width="120"-->
<!--                         align="center">-->
<!--          <template slot-scope="scope">-->
<!--            <udOperation-->
<!--              :data="scope.row"-->
<!--              :permission="permission"-->
<!--            />-->
<!--          </template>-->
<!--        </el-table-column>-->
      </el-table>
      <!--分页组件-->
      <pagination/>
    </div>

    <vue-context-menu :contextMenuData="contextMenuData"
                      @tableConfig="tableConfig"
                      @deletedata="deletedata"></vue-context-menu>

    <!-- 表格配置 -->
    <el-dialog title="表格配置"
               :visible.sync="tableConfigShow"
               width="60%">
      <j-table-column-config :columns.sync="crud.header"  @tabSave="tabSave" @pit="pit" />
    </el-dialog>

    <el-dialog
      title="请选择打印模板"
      :visible.sync="crud.printConfig.pringDialog"
      width="30%"
    >
      <jPrintConfig groupId="feePay"
                    v-if="crud.printConfig.pringDialog"
                    @getPrintId="crud.toPrint"
                    :isPreview="crud.printConfig.isPreview"
      ></jPrintConfig>
    </el-dialog>
  </div>
</template>

<script>
import crudFeePay from '@/api/erp/feePay'
import CRUD, {crud, form, header, presenter} from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import jPrintConfig from '@/components/jPrintConfig/jPrintConfig'
import { publicListKey } from "@/utils/key"
import {getHeaderProperties} from "@/api/erp/headerProperties";
import  jTableColumnConfig from "@/components/jTableColumnConfig/jTableColumnConfig";
import {tablemixins} from "@/mixins/tablemixins";
import {download} from "@/api/data";
import {downloadFile} from "@/utils";

// crud交由presenter持有
const defaultCrud = CRUD({
  title: '费用支付',
  url: 'api/feePay',
  sort: 'id,desc',
  optShow: {
    add: false,
    edit: false,
    del: true,
    copy: false,
    leadingIn: false,
    download: true,
    downloadDetail: false,
    print: true, // 打印
    printDetail: true, // 打印详情
    enable: false,
    disable: false,
    pass: false,
    reject: false
  },
  crudMethod: {...crudFeePay}
})
const defaultForm = {
  id: null,
  code: null,
  name: null,
  type: null,
  busDate: null,
  sourceType: null,
  sourceId: null,
  sourceCode: null,
  customerId: null,
  customerCode: null,
  customerName: null,
  deptId: null,
  deptCode: null,
  deptName: null,
  userId: null,
  userCode: null,
  userName: null,
  payAmount: null,
  isPrint: null,
  printCount: null,
  seq: null,
  orderStatus: null,
  remark: null,
  orderRemark: null,
  createUserId: null,
  createUserCode: null,
  createUserName: null,
  createTime: null,
  updateUserId: null,
  updateUserCode: null,
  updateUserName: null,
  updateTime: null,
  auditUserId: null,
  auditUserCode: null,
  auditUserName: null,
  auditTime: null,
  closeUserId: null,
  closeUserCode: null,
  closeUserName: null,
  closeTime: null
}
export default {
  name: 'FeePay',
  components: {pagination, crudOperation, rrOperation, udOperation, jPrintConfig,jTableColumnConfig,getHeaderProperties},
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud(),publicListKey,tablemixins],
  dicts: ['t_fee_cancel_flow_type', 't_fee_pay_type', 't_fee_pay_source_type', 'common_order_status'],
  data() {
    return {
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      permission: {
        add: ['admin', 'feePay:add'],
        edit: ['admin', 'feePay:edit'],
        del: ['admin', 'feePay:del'],
        import: ['admin', 'feePay:import'],
        export: ['admin', 'feePay:export'],
        copy: ['admin', 'feePay:copy'],
        upload: ['admin', 'feePay:upload'],
        download: ['admin', 'feePay:download'],
        pass: ['admin', 'feePay:pass'],
        reject: ['admin', 'feePay:reject'],
        closed: ['admin', 'feePay:closed'],
        invalid: ['admin', 'feePay:invalid'],
        stop: ['admin', 'feePay:stop'],
        recover: ['admin', 'feePay:recover'],
      },
      userParams: {
        deptId: null
      },
      enable: false,
      able: false
    }
  },
  watch: {},
  methods: {
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }, // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
    },
    changeDept(e) {
      const {id} = e
      this.userParams.deptId = id
    },
    getValue(e) {
      this.query.startTime = this.query.dataExtent[0]
      this.query.endTime = this.query.dataExtent[1]
    },
    exportData() {
      this.crud.query.orderIds =  this.crud.selections.map(item =>item.orderId).join(",")
      const params = {
        orderIds: this.crud.query.orderIds,
        ...this.query
      }
      download('api/feePay/exportImg', params).then(result => {
        downloadFile(result, '费用支付照片', 'xlsx')
        this.crud.downloadDetailLoading = false
      }).catch(() => {
        this.crud.downloadDetailLoading = false
      })
    },
    audit(type,msg) {
      this.crud.rejectLoading = true
      const ids = this.crud.selections.map(m => m.id)
      const data = {
        ids: ids,
        type: type
      }
      crudFeePay.passOrReject(data).then(res => {
        if (200 === res.status) {
          this.crud.refresh()
          this.$message.success(msg+"成功")
        }
        this.crud.rejectLoading = false
      }).catch(e => {
        this.crud.rejectLoading = false
      })
    },
  }
}
</script>

<style scoped>
.table-img {
  display: inline-block;
  text-align: center;
  background: #ccc;
  color: #fff;
  white-space: nowrap;
  position: relative;
  overflow: hidden;
  vertical-align: middle;
  width: 32px;
  height: 32px;
  line-height: 32px;
}
</style>
